/*
 * $Id: StringUtils.java 41 2013-01-08 04:03:56Z rob@patientsknowbest.com $ 
 */
package com.representqueens.util;

/*
 * 
 * Copyright 2006 Larry Ogrodnek
 * Copyright 2011 Rob Whelan for Patients Know Best
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.representqueens.spark.DataPoint;

/**
 * Some simple String utilities.
 * 
 * @author Larry Ogrodnek <larry@cheesesteak.net>
 * @author Rob Whelan <rob@patientsknowbest.com>
 * @version $Revision: 41 $ $Date: 2013-01-08 04:03:56 +0000 (Tue, 08 Jan 2013) $
 */
public final class StringUtils {
    private StringUtils() {
    }

    public static boolean isEmpty(final String s) {
    	return (s == null || "".equals(s.trim()));
    }

    public static List<Integer> toIntList(final String s) {
    	return toIntList(s, "\\s*,\\s*");
    }
    
    public static List<Double> toDoubleList(final String s) {
    	return toDoubleList(s, "\\s*,\\s*");
    }
    
    public static List<String> toStringList(final String s) {
    	return toStringList(s, "\\s*,\\s*");
    }

    public static List<Integer> toIntList(final String s, final String sep) {
		if (isEmpty(s)) {
		    return Collections.emptyList();
		}
	
		final String[] n = s.split(sep);
	
		final List<Integer> ret = new ArrayList<Integer>(n.length);
	
		for (int i = 0; i < n.length; i++) {
		    ret.add(Integer.parseInt(n[i]));
		}
	
		return ret;
    }
    
    public static List<Double> toDoubleList(final String s, final String sep) {
		if (isEmpty(s)) {
		    return Collections.emptyList();
		}
	
		final String[] n = s.split(sep);
	
		final List<Double> ret = new ArrayList<Double>(n.length);
	
		for (int i = 0; i < n.length; i++) {
		    ret.add(Double.parseDouble(n[i]));
		}
	
		return ret;
    }
    
    public static List<String> toStringList(final String s, final String sep) {
		if (isEmpty(s)) {
		    return Collections.emptyList();
		}
	
		final String[] n = s.split(sep);
	
		final List<String> ret = new ArrayList<String>(n.length);
	
		for (int i = 0; i < n.length; i++) {
		    ret.add(String.valueOf(n[i]));
		}
	
		return ret;
    }

    public static String toString(List<DataPoint> data) {
		String s = "";
		for( DataPoint p : data ) {
		    s += p.toString() + ",";
		}
		// snip trailing comma
		if( s.endsWith(",") )
		    s = s.substring(0, s.length()-1);
		
		return s;
    }

}
